public class test {
    class Solution {

        public int ret;

        public TreeNode buildTree(int[] inorder, int[] postorder) {
            ret = postorder.length - 1;
            return createTree(inorder,0,inorder.length - 1,postorder);
        }

        public TreeNode createTree(int[] inorder,int inbegin,int inend,int[] postorder) {
            if(inbegin > inend) {
                return null;
            }
            int num = postorder[ret];
            TreeNode root = new TreeNode(num);
            int rb = findNode(inorder,inbegin,inend,postorder[ret]);
            ret--;

            root.right = createTree(inorder,rb + 1,inend,postorder);
            root.left = createTree(inorder,inbegin,rb - 1,postorder);

            return root;
        }

        public int findNode(int[] array,int a,int b,int x) {
            for(int i = a;i <= b;i++) {
                if(array[i] == x) {
                    return i;
                }
            }
            return -1;
        }
    }
}
